package com.zb.kc.servicebase.exceptionhandler;

import com.zb.kc.commonutils.ExceptionUtil;
import com.zb.kc.commonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 全局默认的异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        log.info(e.getMessage());
        e.printStackTrace();
        return R.err().message("执行了全局异常处理..");
    }

    /**
     * 特定的算数异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R arithmeticException(ArithmeticException e) {
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.err().message("执行了算数异常处理..");
    }

    @ExceptionHandler(KgcException.class)
    @ResponseBody
    public R kgcException(KgcException e) {
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return R.err().message(e.getMsg()).code(e.getCode());
    }

}
